package day15;

import java.io.*;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

/**
 * @author shkstart
 * @creat 2022-05-19:52
 */
public class StudentClient {
    public static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket("localhost",8889);
            OutputStream os = socket.getOutputStream();
            InputStream is = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
           exit: while (true){
                System.out.println("-------------menu-----------");
                System.out.println("---输出全部学生信息请选<1>---");
                System.out.println("---  查询学生信息请选  <2>---");
                System.out.println("请输入：（0退出）");
                int choice = sc.nextInt();
                switch (choice){
                    case 0:{
                        os.write(choice);
                        os.flush();
                        System.out.println("断开连接");
                        break exit;
                    }
                    case 1:{
                        os.write(choice);
                        os.flush();
                        List<Student> students = (List)ois.readObject();
                        for (Student student:students){
                            System.out.println(student);
                        }

                        break;
                    }
                    case 2:{
                        os.write(choice);
                        os.flush();
                        System.out.println("输入姓名：");
                        String name = sc.next();
                        bw.write(name);
                        bw.write("");
                        bw.flush();
                        Student student=(Student)ois.readObject();
                        if(student == null){
                            System.out.println("没有这位同学信息");
                            break;
                        }
                        System.out.println(student);
                        break;
                    }
                    default:{
                        System.out.println("无效数字！！");
                        break;
                    }

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
